/**
 * author: liduanjie
 * usage: 
 * 1、<label for="id of input"><input id="">
 * 2、 data-type required
 * 3、 init required
 * 
 */

$(function() {
    var checkObj = function($row) {
        var $rowCheck = $row.find('input[data-type="row"]');
        var $singleCheck = $row.find('input[data-type="singleCheck"]');
        var $singleChecked = $row.find('input[data-type="singleCheck"]:checked');
        var $allRowCheck = $row.closest('.component-panel').find('input[data-type="row"]');
        var $allRowChecked = $row.closest('.component-panel').find('input[data-type="row"]:checked');
        var $panelCheck = $row.closest('.component-panel').find('input[data-type="panel"]');

        var that = this;

        this.data= {
            singleCheckLen: $singleCheck.length,
            singleUncheckLen: 0,
            allRowUncheckedLen: 0,
            groupCheck: false,
            rowCheck: false,
            panelCheck: false,
            once: true
        };

        this.print = function() {
            console.log('sinleUncheckedLen:' + that.data.singleUncheckLen)
            console.log('allrowUncheckedLen:' + that.data.allRowUncheckedLen)
        }

        // that.print();

        this.update = function() {
            if (that.data.singleUncheckLen) {
                $rowCheck.prop('checked', false);
                $panelCheck.prop('checked', false);
            } else {
                $rowCheck.prop('checked', true);
            }
        }

        this.update1 = function() {

            if($this.is(':checked')) {
                that.data.rowCheck = true;
                $singleCheck.prop('checked', that.data.rowCheck)
            } else {
                that.data.rowCheck = false;
                $singleCheck.prop('checked', that.data.rowCheck)
            }
            
            if (that.data.allRowUncheckedLen) {
                $panelCheck.prop('checked', false);
            } else {
                $panelCheck.prop('checked', true);
            }
        }

        $singleCheck.on('click', function() {
            that.data.singleUncheckLen = $singleCheck.length - $singleChecked.length;
            $rowCheck.prop('checked', true);
            $panelCheck.prop('checked', true);
            // that.update();
        }) 

        $rowCheck.on('click', function() {
            that.data.allRowUncheckedLen = $allRowCheck.length - $allRowChecked.length;
            $this = $(this);
            that.update1($this);
            $panelCheck.prop('checked', true);
        })

        $panelCheck.on('click',function() {
            if ($(this).is(':checked')) {
                that.data.panelCheck = true;
                $rowCheck.prop('checked', that.data.panelCheck);
                $singleCheck.prop('checked', that.data.panelCheck);
            } else {
                that.data.panelCheck = false;
                $rowCheck.prop('checked', that.data.panelCheck);
                $singleCheck.prop('checked', that.data.panelCheck);
            }
        })
    }

    $('.check-row').each(function() {
        var checkbox = new checkObj($(this));
    })

})
